<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form text-select-none" auto-complete="on" label-position="left">

      <div class="title-container">
        <!-- <h3 class="title">登录</h3> -->
      </div>

      <el-form-item prop="username">
        <material-input
          ref="username"
          v-model="loginForm.username"
          name="username"
          @keyup.enter.native="handleLogin"
        >
          用户名
        </material-input>
        <span class="show-right">
          <svg-icon icon-class="user" />
        </span>
      </el-form-item>

      <el-form-item prop="password">
        <material-input
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          name="password"
          @keyup.enter.native="handleLogin"
        >
          密码
        </material-input>
        <span class="show-right" style="cursor: pointer;" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <el-form-item prop="code">
        <material-input
          ref="code"
          v-model="loginForm.code"
          name="code"
          @keyup.enter.native="handleLogin"
        >
          验证码
        </material-input>
        <span class="show-right">
          <svg-icon icon-class="verification-code" />
        </span>
      </el-form-item>

      <div class="bottom-container clearfix">
        <identify-code v-model="loginForm.code" class="identify-code fl" />
        <el-button :loading="loading" type="primary" size="medium" class="fr" @click.native.prevent="handleLogin">登录</el-button>
      </div>

      <div class="bottom-container clearfix">
        <el-checkbox v-model="autoLogin" class="operation fl">下次自动登录</el-checkbox>
        <el-button type="text" size="medium" class="operation fr" @click.native.prevent="handleLogin">忘记密码?</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
// import { validUsername } from '@/utils/validate'
import MaterialInput from '@/components/MaterialInput'
import IdentifyCode from '@/components/IdentifyCode'

// 临时用户
const users = {
  admin: 'marbles!2#',
  developer: 'marbles!2#',
  marbles: 'G00gle.. '
}

export default {
  name: 'Login',
  components: {
    MaterialInput,
    IdentifyCode
  },
  data() {
    const validateUsername = (rule, value, callback) => {
      // callback()
      if (!users[value.trim()]) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不能少于6位'))
      } else if (!(users[this.loginForm.username.trim()] && users[this.loginForm.username.trim()] === value)) {
        callback(new Error('请输入正确的密码'))
      } else {
        callback()
      }
    }
    const validateCode = (rule, value, callback) => {
      if (value !== this.loginForm.code) {
        callback(new Error('请输入正确的验证码'))
      } else {
        callback()
      }
    }
    return {
      code: '',
      loginForm: {
        username: '',
        password: '',
        code: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
        code: [{ required: true, trigger: 'blur', validator: validateCode }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      autoLogin: false
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || '/' })
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 处理input 背景不协调 和光标变色 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    .el-form-item__content {
      line-height: 30px;
      .material-input {
        padding-right: 36px;
      }
    }
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  overflow: hidden;
  background: $bg;
  // background: -webkit-linear-gradient(to right, $bg, #1890ff, #304156);
  // background: linear-gradient(to right, $bg, #1890ff, #304156);
  // background: linear-gradient(to right, #fcb045, #fd1d1d, #833ab4);
  // https://w.wallhaven.cc/full/5w/wallhaven-5wzo39.jpg
  // https://w.wallhaven.cc/full/4g/wallhaven-4g2y7e.jpg
  // https://w.wallhaven.cc/full/3k/wallhaven-3k2y79.jpg
  // background: url("https://w.wallhaven.cc/full/5w/wallhaven-5wzo39.jpg") center center no-repeat fixed;
  canvas {
    position: absolute;
    top: 0px;
    left: 0px;
    z-index: 0;
  }

  .login-form {
    position: absolute;
    top: 200px;
    left: 50%;
    width: 400px;
    max-width: 100%;
    padding: 35px 35px 0 35px;
    margin-left: -200px;
    overflow: hidden;
    background: #fff;
    opacity: 0.85;
    color: $bg;
    border-radius: 4px;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-right {
    position: absolute;
    right: 0;
    top: 30px;
    font-size: 16px;
    color: $dark_gray;

    padding: 0 10px;
    user-select: none;
  }

  .bottom-container {
    margin-top: 30px
  }

  .operation {
    margin-top: 10px;
    margin-bottom: 35px;
    padding-top: 0;
    padding-bottom: 0;
  }
}
</style>
